/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package ar.edu.itba.it.cg.grupo01.game.terrain;

import ar.edu.itba.it.cg.grupo01.utils.Utils;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.SharedMesh;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Cylinder;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

/**
 * ForceFieldFence creates a new Node that contains all the objects that make up
 * the fence object in the game.
 * 
 * @author Mark Powell
 * 
 */
public class Barrier extends Node {
	private static final long serialVersionUID = 1L;

	// The texture that makes up the "force field", we will keep a reference to
	// it
	// here to allow us to animate it.
	private Texture t;

	/**
	 * create the fence, passing the name to the parent.
	 * 
	 * @param name
	 *            the name of the fence
	 */
	public Barrier(String name) {
		super(name);
		buildFence();
	}

	/**
	 * we add an update method to allow the texture to animate. This will be
	 * called from the main games update method.
	 * 
	 * @param interpolation
	 *            the time between frames.
	 */
	public void update(float interpolation) {
		// We will use the interpolation value to keep the speed
		// of the forcefield consistent between computers.
		// we update the Y have of the texture matrix to give
		// the appearance the forcefield is moving.
		t.getTranslation().y += 0.3f * interpolation;
		// if the translation is over 1, it's wrapped, so go ahead
		// and check for this (to keep the vector's y value from getting
		// too large.)
		if (t.getTranslation().y > 1) {
			t.getTranslation().y = 0;
		}
	}

	/**
	 * create all the fence geometry.
	 * 
	 */
	private void buildFence() {

		// Create the actual forcefield
		// The first box handles the X-axis, the second handles the z-axis.
		// We don't rotate the box as a demonstration on how boxes can be
		// created differently.
		Box forceFieldX = new Box("forceFieldX", new Vector3f(-16, -3f, -0.1f),
				new Vector3f(16f, 3f, 0.1f));
		forceFieldX.setModelBound(new BoundingBox());
		forceFieldX.updateModelBound();
		// We are going to share these boxes as well
		SharedMesh forceFieldX1 = new SharedMesh("forceFieldX1", forceFieldX);
		forceFieldX1.setLocalTranslation(new Vector3f(16, 0, 0));
		SharedMesh forceFieldX2 = new SharedMesh("forceFieldX2", forceFieldX);
		forceFieldX2.setLocalTranslation(new Vector3f(16, 0, 32));

		// The other box for the Z axis
		Box forceFieldZ = new Box("forceFieldZ", new Vector3f(-0.1f, -3f, -16),
				new Vector3f(0.1f, 3f, 16));
		forceFieldZ.setModelBound(new BoundingBox());
		forceFieldZ.updateModelBound();
		// and again we will share it
		SharedMesh forceFieldZ1 = new SharedMesh("forceFieldZ1", forceFieldZ);
		forceFieldZ1.setLocalTranslation(new Vector3f(0, 0, 16));
		SharedMesh forceFieldZ2 = new SharedMesh("forceFieldZ2", forceFieldZ);
		forceFieldZ2.setLocalTranslation(new Vector3f(32, 0, 16));

		// add all the force fields to a single node and make this node part of
		// the transparent queue.
		Node forceFieldNode = new Node("forceFieldNode");
		forceFieldNode.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
		forceFieldNode.attachChild(forceFieldX1);
		forceFieldNode.attachChild(forceFieldX2);
		forceFieldNode.attachChild(forceFieldZ1);
		forceFieldNode.attachChild(forceFieldZ2);

		// Add the alpha values for the transparent node
		BlendState as1 = DisplaySystem.getDisplaySystem().getRenderer()
				.createBlendState();
		as1.setBlendEnabled(true);
		as1.setSourceFunction(BlendState.SourceFunction.Zero);
		as1.setDestinationFunction(BlendState.DestinationFunction.One);
		as1.setTestEnabled(true);
		as1.setTestFunction(BlendState.TestFunction.Always);
		as1.setEnabled(true);
		
		forceFieldNode.setRenderState(as1);

		// Attach all the pieces to the main fence node
		this.attachChild(forceFieldNode);
	}
}
